<template>
	<view class="uni-flex uni-column uni-bg-white" 
		style="padding:0 64rpx;height:100%;width:100%;justify-content:space-between;">
		<view style="padding:32rpx 0;">
			<view class="uni-flex uni-row" 
				style="border-bottom: 1rpx solid #D2D2D2;justify-content:space-between;">
				<view style="display:flex;align-items:center;font-size:10pt;color:#181818;">
					<text>厂商图片</text>
				</view>
				<view class="uni-flex uni-row" @click="uploadAvatar">
					<view>
						<image style="width:90rpx;height:90rpx;border-radius:50%;"
							:src="view.avatar"></image>
					</view>
					<view class="uni-flex uni-column" style="justify-content:center;">
						<image style="width:40rpx;height:40rpx;" src="../../static/image/chevron-right.png"></image>
					</view>
				</view>
			</view>
			<view>
				<view class="uni-flex uni-row"
					style="border-bottom: 1rpx solid #DDDDDD;justify-content:space-between;">
					<view style="display:flex;align-items:center;font-size:10pt;color:#181818;">
						<text>名称</text>
					</view>
					<view class="uni-flex uni-row" style="height:90rpx;align-items:center;">
						<view>
							<input style="text-align:right;" v-model="factory.name" maxlength="20"/>
						</view>
					</view>
				</view>
			</view>
			<view>
				<view class="uni-flex uni-row"
					style="border-bottom: 1rpx solid #DDDDDD;justify-content:space-between;">
					<view style="display:flex;align-items:center;font-size:10pt;color:#181818;">
						<text>联系人</text>
					</view>
					<view class="uni-flex uni-row" style="height:90rpx;align-items:center;">
						<view>
							<input style="text-align:right;" v-model="factory.linkman" maxlength="20"/>
						</view>
					</view>
				</view>
			</view>
			<view>
				<view class="uni-flex uni-row"
					style="border-bottom: 1rpx solid #DDDDDD;justify-content:space-between;">
					<view style="display:flex;align-items:center;font-size:10pt;color:#181818;">
						<text>联系电话</text>
					</view>
					<view class="uni-flex uni-row" style="height:90rpx;align-items:center;">
						<view>
							<input style="text-align:right;" v-model="factory.phone" maxlength="20"/>
						</view>
					</view>
				</view>
			</view>
		</view>
		<view style="padding:64rpx 0;">
			<button type="primary" style="font-size:11pt;background-color:#1A75EA;"
				@click="save">保存</button>
		</view>
	</view>
</template>

<script>
	import { API, image_url } from "@/util/constant.js";
	import { post } from '@/util/request.js';
	export default {
		data() {
			return {
				view: {
					avatar: 'https://img2.baidu.com/it/u=1477560808,3458786520&fm=253&fmt=auto&app=138&f=JPEG?w=750&h=500'
				},
				factory: {
					name: '',
					linkman: '',
					phone: '',
					avatar:''
				}
			}
		},
		onLoad() {
		},
		methods: {
			save() {
				if (this.factory.name == '') {
					uni.showToast({
						icon: 'none',
						title: '请输入名称'
					});
					return false;
				}
				
				post('uc/user/update', this.user).then(res => {
					if (res.data) {
						let userInfo = uni.getStorageSync('userInfo');
						userInfo.userName = this.user.userName;
						userInfo.avatar = this.user.avatar;
						uni.setStorageSync('userInfo', userInfo);
						uni.reLaunch({
							url: '/pages/mine/mine'
						})
					} else {
						uni.showToast({
							icon: 'none',
							title: '服务开小差，请稍后再试'
						});
					}
				});
			},
			uploadAvatar() {
				var that = this;
				uni.chooseImage({
					count: 1, //默认9
					sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
					sourceType: ['album','camera'], //从相册选择
					success: function (res) {
						uni.uploadFile({
							url: API +'setting/file/upload', //仅为示例，非真实的接口地址
							filePath: res.tempFilePaths[0],
							name: 'file',
							success(content) {
								if (content.data != '') {
									let result = JSON.parse(content.data);
									that.view.avatar = image_url + result.data[0].fileUrl;
									that.user.avatar = result.data[0].fileUrl;
								}
							},
							fail(error) {
								cosole.log(error)
							}
						})
					}
				});
			}
		}
	}
</script>

<style>
page {
	height:100%;
	width:750rpx;
	display:flex;
}
</style>
